// 表格唯一标识
const gridManagerName = 'files';

const management = {
	
	/**
	 * 初始化表格
	 */
	initGM: function() {
		new window.GridManager(table, {
			gridManagerName: 'files',
			width: '100%',
			height: '100%',
			ajaxData: 'http://localhost:8080/netdisc/admin/admin/allDocumentOfUser?userId=' + window.location.search.split('=')[1],
			ajaxType: 'GET',
			columnData: [
				{
					key: 'id',
					text: '文件id'
				}, {
					key: 'name',
					text: '文件名',
				}, {
					key: 'isDirectory',
					text: '是否为目录'
				}, {
					key: 'isRoot',
					text: '是否为根目录'
				}, {
					key: 'md5',
					text: 'md5'
				}, {
					key: 'size',
					text: '文件大小'
				}, {
					key: 'parentId',
					text: '父目录id'
				}, {
					key: 'isDelete',
					text: '是否删除'
				}
			]
		})
		
	}
}

// GridManager 渲染
const table = document.querySelector('#files');
management.initGM(table);

//获取选中的用户
function getCheckedDocuments() {
	let checkedData = GridManager.getCheckedData(gridManagerName);
	let documentIds = [];
	for(let i = 0; i < checkedData.length; i ++) {
		documentIds.push(checkedData[i].id);
	}
	console.log(documentIds);
	return JSON.stringify(documentIds);
}

function deleteDocument() {
	if( ! confirm("确定删除吗？")) {
		return;
	}
	let documentIds = getCheckedDocuments();
	httpPostWithParams("/admin/setAllDocumentInvisible", documentIds,handler)
}

function undeleteDocument() {
	if( ! confirm("确定取消删除吗？")) {
		return;
	}
	let documentIds = getCheckedDocuments();
	httpPostWithParams("/admin/setAllDocumentVisible", documentIds,handler)
}

function exit() {
	setCookie("userId", "", - 1);
	setCookie("username", "", - 1);
	setCookie("character", "", - 1);
	location.href = "login.html";
}

function setCookie(cname, cvalue, exdays) {
	let d = new Date();
	d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
	let expires = "expires=" + d.toUTCString();
	document.cookie = cname + "=" + cvalue + "; " + expires + ";" + "path=/";
}

function handler(resp) {
	console.log(resp);
	let result = JSON.parse(resp);
	alert(result.message);
	GridManager.refreshGrid(gridManagerName);
}